<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Pattern replace token filter | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'analysis-pattern_replace-tokenfilter.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/analysis-pattern_replace-tokenfilter.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/analysis-pattern_replace-tokenfilter.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/analysis-pattern_replace-tokenfilter.html" rel="nofollow" target="_blank">../en/analysis-pattern_replace-tokenfilter.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="analysis.html">Text analysis</a></span>
»
<span class="breadcrumb-link"><a href="analysis-tokenfilters.html">Token filter reference</a></span>
»
<span class="breadcrumb-node">Pattern replace token filter</span>
</div>
<div class="navheader">
<span class="prev">
<a href="analysis-pattern-capture-tokenfilter.html">« Pattern capture token filter</a>
</span>
<span class="next">
<a href="analysis-phonetic-tokenfilter.html">Phonetic token filter »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="analysis-pattern_replace-tokenfilter"></a>Pattern replace token filter<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/tokenfilters/pattern_replace-tokenfilter.asciidoc">edit</a>
</h2>
</div></div></div>

<p>Uses a regular expression to match and replace token substrings.</p>
<p>The <code class="literal">pattern_replace</code> filter uses
<a href="http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html" class="ulink" target="_top">Java’s
regular expression syntax</a>. By default, the filter replaces matching
substrings with an empty substring (<code class="literal">""</code>).</p>
<p>Replacement substrings can use Java’s
<a href="https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#appendReplacement-java.lang.StringBuffer-java.lang.String-" class="ulink" target="_top"><code class="literal">$g</code> syntax</a> to reference capture groups
from the original token text.</p>
<div class="warning admon">
<div class="icon"></div>
<div class="admon_content">
<p>A poorly-written regular expression may run slowly or return a
StackOverflowError, causing the node running the expression to exit suddenly.</p>
<p>Read more about
<a href="http://www.regular-expressions.info/catastrophic.html" class="ulink" target="_top">pathological regular
expressions and how to avoid them</a>.</p>
</div>
</div>
<p>This filter uses Lucene’s
<a href="https://lucene.apache.org/core/8_5_1/analyzers-common/org/apache/lucene/analysis/pattern/PatternReplaceFilter.html" class="ulink" target="_top">PatternReplaceFilter</a>.</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="analysis-pattern-replace-tokenfilter-analyze-ex"></a>Example<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/tokenfilters/pattern_replace-tokenfilter.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The following <a class="xref" href="indices-analyze.html" title="Analyze API">analyze API</a> request uses the <code class="literal">pattern_replace</code>
filter to prepend <code class="literal">watch</code> to the substring <code class="literal">dog</code> in <code class="literal">foxes jump lazy dogs</code>.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_analyze
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "pattern_replace",
      "pattern": "(dog)",
      "replacement": "watch$1"
    }
  ],
  "text": "foxes jump lazy dogs"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/967.console"></div>
<p>The filter produces the following tokens.</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">[ foxes, jump, lazy, watchdogs ]</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="analysis-pattern-replace-tokenfilter-configure-parms"></a>Configurable parameters<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/tokenfilters/pattern_replace-tokenfilter.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">all</code>
</span>
</dt>
<dd>
(Optional, boolean)
If <code class="literal">true</code>, all substrings matching the <code class="literal">pattern</code> parameter’s regular expression
are replaced. If <code class="literal">false</code>, the filter replaces only the first matching substring
in each token. Defaults to <code class="literal">true</code>.
</dd>
<dt>
<span class="term">
<code class="literal">pattern</code>
</span>
</dt>
<dd>
(Required, string)
Regular expression, written in
<a href="http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html" class="ulink" target="_top">Java’s
regular expression syntax</a>. The filter replaces token substrings matching this
pattern with the substring in the <code class="literal">replacement</code> parameter.
</dd>
<dt>
<span class="term">
<code class="literal">replacement</code>
</span>
</dt>
<dd>
(Optional, string)
Replacement substring. Defaults to an empty substring (<code class="literal">""</code>).
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="analysis-pattern-replace-tokenfilter-customize"></a>Customize and add to an analyzer<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/tokenfilters/pattern_replace-tokenfilter.asciidoc">edit</a>
</h3>
</div></div></div>
<p>To customize the <code class="literal">pattern_replace</code> filter, duplicate it to create the basis
for a new custom token filter. You can modify the filter using its configurable
parameters.</p>
<p>The following <a class="xref" href="indices-create-index.html" title="Create index API">create index API</a> request
configures a new <a class="xref" href="analysis-custom-analyzer.html" title="Create a custom analyzer">custom analyzer</a> using a custom
<code class="literal">pattern_replace</code> filter, <code class="literal">my_pattern_replace_filter</code>.</p>
<p>The <code class="literal">my_pattern_replace_filter</code> filter uses the regular expression <code class="literal">[£|€]</code> to
match and remove the currency symbols <code class="literal">£</code> and <code class="literal">€</code>. The filter’s <code class="literal">all</code>
parameter is <code class="literal">false</code>, meaning only the first matching symbol in each token is
removed.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "keyword",
          "filter": [
            "my_pattern_replace_filter"
          ]
        }
      },
      "filter": {
        "my_pattern_replace_filter": {
          "type": "pattern_replace",
          "pattern": "[£|€]",
          "replacement": "",
          "all": false
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/968.console"></div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="analysis-pattern-capture-tokenfilter.html">« Pattern capture token filter</a>
</span>
<span class="next">
<a href="analysis-phonetic-tokenfilter.html">Phonetic token filter »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>